<?php

namespace Biz\model\GoodsTestActivity;

use Doraemon\pockets\datebase\BaseDbModel;
use Medoo\Medoo;

class FineGoodsJoinModel extends BaseDbModel
{
    public const _CONFIG_FILE_ = 'db_store';

    public const _CONFIG_SELECT_ = 'db_brand_activity';

    public const _DATABASE_ = 'brand_activity';

    public const _TABLE_NAME = 'fine_goods_test_activity_join';

    public const JOIN_STATUS_NOT_DEFAULT = 0; //默认
    public const JOIN_STATUS_NOT_PREFER = 1; //备选
    public const JOIN_STATUS_PREFER = 2;//首选
    public const JOIN_STATUS_DEL = 9;//删除
    public const LOGISTICS_STATUS_NO_SHIPPED = 1;//未发货
    public const LOGISTICS_STATUS_SHIPPED = 2;//已发货


    public const DELIVERED_TIME_PUSH = 7 * 24 * 3600;//快递已签收且第7天内仍未产出内容 发送push
    public const DELIVERED_TIME_PUSH_SIX = 6 * 24 * 3600;//快递已签收且第6天内仍未产出内容 消息通知
    public const DELIVERED_TIME = 8 * 24 * 3600;

    const JOIN_STATUS_DESC = [
        self::JOIN_STATUS_NOT_DEFAULT => '默认',
        self::JOIN_STATUS_NOT_PREFER => '备选',
        self::JOIN_STATUS_PREFER => '首选',
        self::JOIN_STATUS_DEL => '取消',
    ];

    const LOGISTICS_STATUS_MAP = [
        self::LOGISTICS_STATUS_NO_SHIPPED => '未发货',
        self::LOGISTICS_STATUS_SHIPPED => '已发货',
    ];


    /**
     * 获取一行
     * @param $where
     * @param string $field
     * @return array|mixed|null
     */
    public static function first($where, $field = '*')
    {
        $rs = self::get($field, $where);
        if (!$rs) {
            return null;
        }

        return $rs;
    }

    /**
     * 批量查询
     * @param $where
     * @param  $field
     * @return array|bool
     */
    public static function all($where, $field = '*')
    {
        return self::select($field, $where);
    }

    /**
     * 获取一列的值
     * @param $where
     * @param array $field
     * @return array|bool
     */
    public static function pluck($where, array $field = ['id'])
    {
        return self::select($field, $where);
    }

    /**
     * 从记录中获取单个值
     * @param $where
     * @param string $columns
     * @return array|bool
     */
    public static function value($where, string $columns = 'id')
    {
        return self::get($columns, $where);
    }

    /**
     * 获取首选名单
     * @param $id
     * @param string $field
     * @return array|bool
     */
    public static function getLastPersons($id, $field = "*")
    {
        $where['join_status'] = self::JOIN_STATUS_PREFER;
        $where['fine_goods_id'] = $id;

        return self::all($where, $field);
    }

    /**
     * 查询活动选中名额的数量
     * @param $fineGoodsId
     * @return bool|int|mixed|string
     */
    public static function getConfirmedNum($fineGoodsId)
    {
        $confirmNum = FineGoodsJoinModel::count([
            'fine_goods_id' => $fineGoodsId,
            'join_status' => self::JOIN_STATUS_PREFER,
        ]);

        return $confirmNum;
    }


    public static function insertData($data)
    {
        if (!isset($data['addtime'])) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return self::id();
    }

    public static function updateData($data, $where)
    {
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return true;
    }

    public static function getCountMaps($map)
    {
        return self::select(['cnt' => Medoo::raw('count(fine_goods_id)'), 'fine_goods_id'], $map);
    }
}